package other.example;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fangbin
 * @desc
 * @date 2020/5/30 9:50
 */
public class CustomFile {

    public static void main(String[] args) throws IOException, ClassNotFoundException {

        File file = new File("D:\\test2.dmp");
/*
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
        List<Student> studentList = new ArrayList<>();
        for(int i=0;i<100;i++){
            studentList.add(new Student(i+"",i+""));
        }
        objectOutputStream.writeObject(studentList);
        objectOutputStream.close();
        fileOutputStream.close();
*/

        FileInputStream fileInputStream = new FileInputStream(file);
        ObjectInputStream objectInputStream  =  new ObjectInputStream(fileInputStream);
        List<Student> students = (List<Student>) objectInputStream.readObject();
        for(Student student:students){
            System.out.println(student);
        }

        objectInputStream.close();
        fileInputStream.close();
    }
}

class Student implements Serializable {

    private String username;
    private String password;

    public Student(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "Student{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
